﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Windows.Devices.Geolocation;

namespace RadarGlobal.UIView
{
    public partial class TrackingLocationDlg : PhoneApplicationPage
    {
        public TrackingLocationDlg()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (App.Geolocator == null)
            {
                App.Geolocator = new Geolocator();
                App.Geolocator.DesiredAccuracy = PositionAccuracy.High;
                App.Geolocator.MovementThreshold = 100; // 100 meters
                App.Geolocator.StatusChanged += geolocator_StatusChanged;
                App.Geolocator.PositionChanged += geolocator_PositionChanged;
            }
        }
        void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
        {
            string positionString = this.GetCoordinateString(args.Position.Coordinate);
            if (!App.RunningInBackground)
            {
                // IMPORTANT: the event handler is called on a worker thread,
                // so we must use the Dispatcher to update the UI from the main thread
                Dispatcher.BeginInvoke(() =>
                {
                    this.txtLocation.Text = positionString;
                });
            }
            else
            {
                Microsoft.Phone.Shell.ShellToast toast = new Microsoft.Phone.Shell.ShellToast();
                toast.Content = positionString;
                toast.Title = "Location: ";
                toast.NavigationUri = new Uri("/MainPage.xaml", UriKind.Relative);
                toast.Show();
            }
        }
        private string GetCoordinateString(Geocoordinate coordinate)
        {
            string positionString = string.Format("Lat: {0:0.0000}, Long: {1:0.0000}, Acc: {2}m",
                        coordinate.Latitude, coordinate.Longitude, coordinate.Accuracy);
            return positionString;
        }
        private void geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
        {
            if (App.RunningInBackground)
            {
                    return;
            }
            string status = "";
 
            switch (args.Status)
            {
                case PositionStatus.Disabled:
                // the application does not have the right capability or the location master switch is off
                status = "location is disabled in phone settings";
                break;
 
                case PositionStatus.Initializing:
                // the geolocator started the tracking operation
                status = "initializing";
                break;
 
                case PositionStatus.NoData:
                // the location service was not able to acquire the location
                status = "no data";
                break;
 
                case PositionStatus.Ready:
                // the location service is generating geopositions as specified by the tracking parameters
                status = "ready";
                break;
 
                case PositionStatus.NotAvailable:
                status = "not available";
                // not used in WindowsPhone, Windows desktop uses this value to signal that there is no hardware capable to acquire location information
                break;
 
                case PositionStatus.NotInitialized:
                // the initial state of the geolocator, once the tracking operation is stopped by the user the geolocator moves back to this state
                status = "not initialized";
                break;
            }
            // IMPORTANT: the event handler is called on a worker thread,
            // so we must use the Dispatcher to update the UI from the main thread
            Dispatcher.BeginInvoke(() =>
            {
                this.txtStatus.Text = status;
            });
        }
        protected override void OnRemovedFromJournal(JournalEntryRemovedEventArgs e)
        {
            this.StopLocationTracking();
        }

        private void StopLocationTracking()
        {
            if (App.Geolocator != null)
            {
                App.Geolocator.PositionChanged -= geolocator_PositionChanged;
                App.Geolocator.StatusChanged -= geolocator_StatusChanged;
                App.Geolocator = null;
            }
        }
    }
}